package com.hyp.pay.UI.Home.presenter;

import android.util.Log;

import com.hyp.pay.UI.Home.presenter.contract.PictureContract;
import com.hyp.pay.base.RxPresenter;
import com.hyp.pay.http.Constants;
import com.hyp.pay.http.RetrofitHelper;
import com.hyp.pay.model.FindRecharge;
import com.hyp.pay.model.UplodIdcard;
import com.hyp.pay.model.UserJson;

import org.json.JSONException;

import java.io.File;

import javax.inject.Inject;

import io.reactivex.Flowable;
import io.reactivex.subscribers.ResourceSubscriber;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by Administrator on 2017/6/2.
 */

public class PicturePresenter extends RxPresenter<PictureContract.View> implements PictureContract.Presenter{

    private RetrofitHelper mRetrofitHelper;

    @Inject
    public PicturePresenter(RetrofitHelper mRetrofitHelper) {
        this.mRetrofitHelper = mRetrofitHelper;
    }




    @Override
    public void getUploadFilePicture(String filePath, int userid) {
        File file = new File(filePath);


        RequestBody requestFile = RequestBody.create(MediaType.parse("image/*"), file);



        MultipartBody.Part body =
                MultipartBody.Part.createFormData("myfile", file.getName(), requestFile);



        Flowable<UplodIdcard> flowable = mRetrofitHelper.UploadFilePicture(body,userid);
        ResourceSubscriber subscriber = mRetrofitHelper.startObservable(flowable, new ResourceSubscriber<UplodIdcard>() {

            @Override
            public void onNext(UplodIdcard data) {

                Log.i("olj",data.toString());
                if (getView() != null) {
                    getView().showUplod(data);
                }

            }

            @Override
            public void onError(Throwable t) {
                t.printStackTrace();
                if (getView() != null)
                    getView().showError(Constants.GRS_ERROR);
            }

            @Override
            public void onComplete() {

            }
        });

        addSubscription(subscriber);
    }

    @Override
    public void getFindRecharge(String charge) {
        Flowable<FindRecharge> flowable = mRetrofitHelper.findRecharge(charge);
        ResourceSubscriber subscriber = mRetrofitHelper.startObservable(flowable, new ResourceSubscriber<FindRecharge>() {

            @Override
            public void onNext(FindRecharge data) {

                Log.i("olj",data.toString());

                if (getView() != null) {
                    try {
                        getView().showFindRecharge(data);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

            }

            @Override
            public void onError(Throwable t) {
                t.printStackTrace();
                if (getView() != null)
                    getView().showError(Constants.GRS_ERROR);
            }

            @Override
            public void onComplete() {

            }
        });

        addSubscription(subscriber);
    }

    @Override
    public void getUsr(String userid) {
        Flowable<UserJson> flowable = mRetrofitHelper.findUser(userid);
        ResourceSubscriber subscriber = mRetrofitHelper.startObservable(flowable, new ResourceSubscriber<UserJson>() {

            @Override
            public void onNext(UserJson data) {

                Log.i("olj",data.toString());
                if (getView() != null) {
                    getView().showUser(data);
                }

            }

            @Override
            public void onError(Throwable t) {
                t.printStackTrace();
                if (getView() != null)
                    getView().showError(Constants.GRS_ERROR);
            }

            @Override
            public void onComplete() {

            }
        });

        addSubscription(subscriber);
    }
}
